package com.digitalchina.lxnsy.fragment;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.AgriculturalTrainingDetailsActivity;
import com.digitalchina.lxnsy.activity.old.MyAttentionActivity;
import com.digitalchina.lxnsy.api.BusinessAttention;
import com.digitalchina.lxnsy.base.BaseFragment;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.AttentionTrainVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.AttentionTrainingHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 我的关注页面的训练列表
 */
public class MyAttentionTrainingFragment extends BaseFragment implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;
    private final int TRAINING_LIST = 2;
    private List trainList = new ArrayList();
    private int page = 1;
    private int limit = 10;

    @Override
    public View setRootView() {
        return inflate(R.layout.fragment_my_attention);
    }

    @Override
    public void initMember() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(Utils.setSpaceDecoration(getActivity(), false, false, false));
        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new AttentionTrainingHolder(parent);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore);
        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                AttentionTrainVO item = (AttentionTrainVO) adapter.getAllData().get(position);
                Intent intent = new Intent(getActivity(), AgriculturalTrainingDetailsActivity.class);
                intent.putExtra("toTrainDetails", item.getTrain());
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (recyclerView == null) {
                    return;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);

                switch (msg.what) {
                    case MSG.MSG_GET_EXPERT_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_EXPERT_LIST_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        trainList = FastJsonUtil.getListBean(object.toString(), "body", "attList", AttentionTrainVO.class);

        if (page == 1) { // 刷新
            adapter.clear();
        }

        if (trainList != null) {
            if (trainList.size() < limit) {
                adapter.stopMore();
            }
        }

        adapter.addAll(trainList);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 2) {
            // 若有取消关注刷新本页数据和专家列表的数据
            onRefresh();
            ((MyAttentionActivity) getActivity()).notifyToRefresh();
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessAttention.getAttentionList(getActivity(), String.valueOf(TRAINING_LIST), String.valueOf(page), String.valueOf(limit), mHandler);
    }

    @Override
    public void onLoadMore() {
            page++;
            BusinessAttention.getAttentionList(getActivity(), String.valueOf(TRAINING_LIST), String.valueOf(page), String.valueOf(limit), mHandler);
    }

    @Override
    public void rightOnClick() {

    }
}